package net.yb.controller;

import net.yb.entity.User;
import net.yb.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@RequestMapping("/param")
@RestController
public class ParamController {

    /**
     * JavaEE组件
     */
    @RequestMapping("/param1")
    public void param1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println(req.getParameter("name"));

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print("<h1 style='color:red;'>哈哈哈哈</h1>");
        out.close();
    }

    /**
     * String、基本类型及包装类
     */
    @RequestMapping("/param2")
    public AjaxResult param2(@RequestParam("name") String name, @RequestParam(required = false, defaultValue = "18") int age, Double height){
        System.out.println(name);
        System.out.println(age);
        System.out.println(height);

        return AjaxResult.success();
    }

    @RequestMapping("/param3/{id}/{name}")
    public AjaxResult param3(@PathVariable("id") int id, @PathVariable String name){
        System.out.println(id);
        System.out.println(name);

        return AjaxResult.success();
    }

    @RequestMapping("/param4")
    public AjaxResult param4(@RequestBody User user){
        System.out.println(user);
        return AjaxResult.success();
    }

    @RequestMapping("/param5")
    public AjaxResult param5(User user){
        System.out.println(user);
        return AjaxResult.success();
    }
}
